package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.CarouselService;
import com.baichang.taoshunda.service.PaymentManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/1/26 0026.
 */
@Controller
@RequestMapping(ControllerConstants.CAROUSEL)
public class BusinessCarouselMController extends BaseMobileController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private PaymentManagementService paymentManagementService;

    /**
     * 添加轮播图
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addCarousel")
    public void addCarousel(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer busId = getInt(json, "busId", 0); //商家id
                Integer type = getInt(json, "type", 0);
                return carouselService.addCarousel(busId, type);
            }
        });
    }

    /**
     * 选择缴费分类(查询出对应时间的钱)
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryPaymentType")
    public void queryPaymentType(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer type = getInt(json, "type", 0);
                return paymentManagementService.queryPaymentType(type);
            }
        });
    }

    /**
     * 服务商家支付
     *
     * @param request
     * @param response
     */
    @RequestMapping("/paymentMoney")
    public void payMentMoney(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                Integer busId = getInt(json, "busId", 0);
                Integer payType = getInt(json, "payType", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(json, "price", 0));
                Integer status = getInt(json, "status", 0);
                return carouselService.payMentMoney(id, busId, payType, money, status);
            }
        });
    }


    /**
     * 轮播图缴费成功调用
     *
     * @param request
     * @param response
     */
    //TODO 支付回调
    @RequestMapping("/successPaymentMoney")
    public void successPaymentMoney(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                Integer busId = getInt(json, "busId", 0);
                Integer payType = getInt(json, "payType", 0);
                BigDecimal money = BigDecimal.valueOf(getDouble(json, "money", 0));
                Integer month = getInt(json, "month", 0);
                Integer type = getInt(json, "type", 0);
                return carouselService.successPaymentMoney(id, busId, payType, money, month, type);
            }
        });
    }

    /**
     * 查询我的轮播图
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryBusCarouselDetails")
    public void queryBusCarouselDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer busId = getInt(json, "busId", 0);
                Integer type = getInt(json, "type", 0); //1 轮播图 2 红包
                try {
                    return carouselService.queryBusCarouselDetails(busId, type);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    public static void main(String[] args) {
        String DateStr1 = "2018-07-20 10:20:16"; //结束时间
        String DateStr2 = "2018-07-26 15:50:35";  //当前时间
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime1 = null;
        Date dateTime2 = null;
        try {
            dateTime1 = dateFormat.parse(DateStr1);
            dateTime2 = dateFormat.parse(DateStr2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = dateTime1.compareTo(dateTime2);
        System.out.println(i);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

    }
}
